//
//  SellTicketsOperation.m
//  TestObjc
//
//  Created by wp w on 12-6-13.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "SellTicketsOperation.h"

@implementation SellTicketsOperation
@synthesize tickets;
@synthesize ticketsCondition;
@synthesize controller;
@synthesize name;

-(void)dealloc{
    [ticketsCondition release];
    [controller release];
    [name release];
    [super dealloc];
}

- (void)main {
    while (YES) { 
        //上锁 
        [ticketsCondition lock]; 
        if(*tickets > 0){ 
            [NSThread sleepForTimeInterval:0.1]; 
            NSLog(@"%@:当前票数是:%d,线程名:%@",name,*tickets,[[NSThread currentThread] name]); 
            (*tickets)--; 
            
            //多线程编程中普遍遵循一个原则，就是一切与UI相关的操作都有主线程做，子线程只负责事务，数据方面的处理
            //在iPhone中，需要使用performSelectorOnMainThread 委托主线程处理
            [controller performSelectorOnMainThread: @selector(refreshThreadTitle:) withObject:[NSNumber numberWithInt:0] waitUntilDone: NO];
        }else{ 
            [controller performSelectorOnMainThread: @selector(refreshThreadTitle:) withObject:[NSNumber numberWithInt:1] waitUntilDone: NO];
            break; 
        } 
        //解锁 
        [ticketsCondition unlock]; 
    } 
}

@end
